<?php

declare(strict_types=1);

namespace app\admin\controller\h5;

use think\facade\Request;
use app\common\service\H5Server as S;
use app\common\model\H5Server as M;
use app\common\model\AdminAdmin as UM;
use app\common\util\SshConnet;
use think\facade\Filesystem;

class Server extends \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    // 列表
    public function index()
    {
        if (Request::isAjax()) {
            return $this->getJson(M::getList());
        }
        return $this->fetch();
    }

    // 添加
    public function add()
    {
        if (Request::isAjax()) {
            try {
                UM::MoServerName(Request::post('serverid'), Request::post('servername'), Request::post('name'));
                $response = ['msg' => '操作成功', 'code' => 200,];
                return json($response);
            } catch (\Throwable $th) {
                $response = ['msg' => '操作失败', 'code' => 201,];
                return json($response);
            }
        }
        return $this->fetch('add', ['ServerLs' => UM::getServerLsSelect()]);
    }

    // 添加  
    // 记录  上传需要到php中安装 fileinfo扩展  然后更新filesystem扩展(cd 到根目录 直接执行)：composer require topthink/think-filesystem
    public function choujiang()
    {
        if (Request::isAjax()) {
            //  try {
            //      UM::MoServerName(Request::post('serverid'), Request::post('servername'), Request::post('name'));
            //      $response = ['msg' => '操作成功', 'code' => 200,];
            //      return json($response);
            //  } catch (\Throwable $th) {
            //      $response = ['msg' => '操作失败', 'code' => 201,];
            //      return json($response);
            //  }
        }
        return $this->fetch('choujiang');
    }

    // 编辑
    public function edit()
    {
        if (Request::isAjax()) {
            try {
                UM::MoPalyerJob(Request::post('serverid'), Request::post('actorname'), Request::post('jobtype'));
                $response = ['msg' => '操作成功', 'code' => 200,];
                return json($response);
            } catch (\Throwable $th) {
                $response = ['msg' => '操作失败', 'code' => 201,];
                return json($response);
            }
        }
        return $this->fetch('edit', ['ServerLs' => UM::getServerLsSelect()]);
    }

    // 添加新区
    public function addNewServer()
    {
        $key = Request::post('key');
        if ($key == 1) {
            $cmd = '/data/addServer神谕.sh';
        } elseif ($key == 2) {
            $cmd = '/data/addServer重生.sh';
        } elseif ($key == 3) {
            $cmd = '/data/addServer序列.sh';
        } else {
            return json(['msg' => '失败，错误key：' . $key, 'code' => '1',]);
        }
        $msg = SshConnet::excssh($cmd);
        $serverId = $msg["output"];
        if ($serverId % 5 == 0) {
            if ($key == 1) {
                $cmd = '/data/addCrossServer神谕.sh';
            } elseif ($key == 2) {
                $cmd = '/data/addCrossServer重生.sh';
            } elseif ($key == 3) {
                $cmd = '/data/addCrossServer序列.sh';
            }
            $msg = SshConnet::excssh($cmd);
        }
        return json(['msg' => $msg, 'code' => '200',]);
    }

    // 重启
    public function closeServer()
    {
        $sid = Request::post('sid');
        $sid = intval($sid) - 1000;
        $cmd = '/data/mu_s' . $sid . '/stop.sh';
        $msg = SshConnet::connectServer($cmd);
        return json($msg);
    }

    public function openServer()
    {
        $sid = Request::post('sid');
        $sid = intval($sid) - 1000;
        $cmd = '/data/mu_s' . $sid . '/run.sh';
        $msg = SshConnet::connectServer($cmd);
        return json($msg);
    }

    public function openkf()
    {
        $sid = Request::post('sid');
        $filename = UM::getsrvid($sid);
        $cmd = '/data/mu_s' . $filename . '/run.sh';
        $msg = SshConnet::connectServer($cmd);
        return json($msg);
    }

    public function closekf()
    {
        $sid = Request::post('sid');
        $filename = UM::getsrvid($sid);
        $cmd = '/data/mu_s' . $filename . '/stop.sh';
        $msg = SshConnet::connectServer($cmd);
        return json($msg);
    }

    // // 状态
    // public function status($id){
    //     return $this->getJson(S::goStatus(Request::post('status'),$id));
    //     }

    // // 删除
    // public function remove($id){
    //     return $this->getJson(S::goRemove($id));
    //     }

    // 批量删除
    public function batchRemove()
    {
        return $this->getJson(S::goBatchRemove(Request::post('ids'), Request::post('status')));
    }

    // 回收站
    // public function recycle(){
    //     if (Request::isAjax()) {
    //         return $this->getJson(S::goRecycle());
    //     }
    //     return $this->fetch();
    // }

}
